package cn.agiledata.bank.transaction.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.servlet.InitForString;
import cn.agiledata.bank.hibernateBean.SubAccount;
import cn.agiledata.bank.hibernateBean.Transaction;
import cn.agiledata.bank.transaction.form.BankRemitInpourForm;
import cn.agiledata.bank.transaction.form.OfflineInpourForm;

/**
 * 银行汇款充值
 * 
 * @author maxianbo
 * 
 */
public final class BankRemitInpourAction extends OfflineInpourAction {

    public ActionForward prepare(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = super.prepare(mapping, actionForm, request,
                response);
        loadBankList(request);
        return forward;
    }

    private void loadBankList(HttpServletRequest request) {
        // 加载收款银行列表
        request.setAttribute("collectBankList", getCommonService()
                .getBankList());
        // 加载汇款银行列表
        request.setAttribute("remitBankList", InitForString.dataDictMap
                .get("ENTERPRISE_BANK_INFO"));
    }

    public ActionForward fill(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = super.fill(mapping, actionForm, request,
                response);
        if (hasAppExceptionMessage(request)) {
            loadBankList(request);
        } else {
            BankRemitInpourForm form = (BankRemitInpourForm) actionForm;
            // 收款银行
            loadCollectBank(request, form);
            // 汇款银行
            String remitBankId = String.valueOf((form.getRemitBankId()));
            request.setAttribute("remitBank", getCommonService().getBankInfo(
                    remitBankId));
        }
        return forward;
    }

    /**
     * 加载收款银行
     */
    private void loadCollectBank(HttpServletRequest request,
            BankRemitInpourForm form) {
        String collectBankId = String.valueOf(form.getCollectBankId());
        request.setAttribute("collectBank", getCommonService().getBankBeanById(
                collectBankId));
    }

    public ActionForward inpour(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = super.inpour(mapping, actionForm, request,
                response);
        BankRemitInpourForm form = (BankRemitInpourForm) actionForm;
        loadCollectBank(request, form);
        return forward;
    }

    protected Transaction createTransaction(OfflineInpourForm inpourForm) {
        BankRemitInpourForm form = (BankRemitInpourForm) inpourForm;
        Transaction trans = super.createTransaction(form);
        // 开户行id
        trans.setBankSeq(new Long(form.getRemitBankId()));
        // 开户行名称
        trans.setBankName(form.getIssuingBankName());
        // 汇款人姓名
        trans.setPersonName(form.getRemitterName());
        // 汇款账户
        trans.setCardNo(form.getRemitAccountNo());
        // 收款行
        trans.setBankInfoSeq(new Long(form.getCollectBankId()));
        return trans;
    }

    public ActionForward modify(ActionMapping mapping, ActionForm actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ActionForward forward = prepare(mapping, actionForm, request, response);
        BankRemitInpourForm form = (BankRemitInpourForm) actionForm;
        Transaction trans = getTransactionTemplate().getTransactionByTranId(
                form.getTransId());
        loadForm(form, trans);
        if (isCorpAccount(request)) {
            form.setRemitAccountNo(trans.getCardNo());
            form.setRemitterName(trans.getPersonName());
        }
        return forward;
    }

    private void loadForm(BankRemitInpourForm form, Transaction trans) {

        form.setAmount(trans.getAmount().toString());
        form.setNextStep(2);
        form.setRemark(trans.getRemark());
        form.setTransId(trans.getTransId());
        if (trans.getBankCitySeq() != null)
            form.setRemitCityId(trans.getBankCitySeq().toString());
        if (trans.getBankProvSeq() != null)
            form.setRemitProvinceId(trans.getBankProvSeq().toString());
        SubAccount subAccount = getAccountTemplate().getSubAccount(
                trans.getCredit().getId().toString());
        form.setSubAccountId(subAccount.getAccountid());
        form.setCollectBankId(trans.getBankInfoSeq().longValue());
        form.setRemitBankId(trans.getBankSeq().longValue());
        form.setIssuingBankName(trans.getBankName());
        form.setSubAccountId(trans.getCredit().getAccountId());
    }

}
